﻿using System;
using System.Collections.Generic;
using OrderingPlatform.Model;
using OrderingPlatform.DalService.ProductManage;
using OrderingPlatform.Infrastructure;
using OrderingPlatform.Model.ProductManage;
using OrderingPlatform.MvcPager.Mvc;
using OrderingPlatform.MvcExtension;
namespace OrderingPlatform.BllService.ProductService
{
    public class ProductService
    {
        private readonly ProductDal _productDal = new ProductDal();

        public Product GetModel(Guid productId,bool hasVisist=false)
        {
            try
            {
                return _productDal.GetModel(productId,hasVisist);
            }
            catch (Exception)
            {
                return new Product();
            }
        }

        public CallBackModel AddorEdit(Product model)
        {
            var result = new CallBackModel { State = 0 };
            try
            {
                var errorMessage = "";
                if (string.IsNullOrEmpty(model.ProductName))
                {
                    errorMessage += "产品名称不能为空！</br>";
                }
                if (string.IsNullOrEmpty(model.Category))
                {
                    errorMessage += "类别不能为空！</br>";
                }

                if (!string.IsNullOrEmpty(errorMessage))
                {
                    result.Message = errorMessage;
                }
                else
                {
                    _productDal.AddorEdit(model);
                    result.State = 1;
                }
            }
            catch (Exception ex)
            {
                result.Message = "保存失败！";
                result.ErrorMessage = ex.Message;
            }
            return result;
        }

        public CallBackPager<ProductModel> GetList(ProductModel model, int pageIndex, int pageSize)
        {
            try
            {
                var result = new CallBackPager<ProductModel> { Pager = _productDal.GetList(model, pageIndex, pageSize) };
                return result;
            }
            catch (Exception)
            {
                var result = new CallBackPager<ProductModel> { Pager = new PagedList<ProductModel>(new List<ProductModel>(), pageIndex, pageSize, 0) };
                return result;
            }
        }

        public CallBackModel Del(string ids)
        {
            var result = new CallBackModel { State = 0 };
            try
            {
                _productDal.Del(ids);
                result.State = 1;

            }
            catch (Exception ex)
            {
                result.Message = "删除失败！";
                result.ErrorMessage = ex.Message;
            }
            return result;
        }

       

    }
}
